package ARKit;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;

public class MiniMap extends View {

	private ShapeDrawable map, center;
	private Context ctx;
	private static final int radious = 200;
	private static final int padding = 10;
	private float rot = 0.0f;
	private int x, y, pivotX, pivotY;
	private volatile List<ShapeDrawable> points;
	private float max_distance = 10000;
	private Paint p,paint;
	private ShapeDrawable outLine;

	public MiniMap(Context context, int screen_width, int screen_heigth) {
		super(context);
		ctx = context;
		x = screen_width - radious - padding;
		y = screen_heigth - radious - padding;

		pivotX = x + (radious / 2);
		pivotY = y + (radious / 2);

		map = new ShapeDrawable(new OvalShape());
		map.setBounds(x, y, x + radious, y + radious);
		map.getPaint().setColor(Color.WHITE);
		map.setAlpha(95);
		
		outLine = new ShapeDrawable(new OvalShape());
		outLine.setBounds(x, y, x + radious, y + radious);
		outLine.getPaint().setColor(Color.rgb(3, 187, 255));
		outLine.getPaint().setStyle(Style.STROKE);
		outLine.getPaint().setStrokeWidth(2);
		outLine.getPaint().setAntiAlias(true);
		
		
		center = new ShapeDrawable(new OvalShape());
		center.setBounds(pivotX - 3, pivotY - 3, pivotX + 3, pivotY + 3);
		center.getPaint().setColor(Color.BLACK);
		p = new Paint();
		p.setColor(Color.rgb(3, 187, 255));
		p.setAntiAlias(true);
		
		paint = new Paint();
		paint.setColor(Color.rgb(3, 187, 255));
		paint.setAntiAlias(true);
		paint.setTextSize(18);
		paint.setTextAlign(Align.CENTER);

		points = new ArrayList<ShapeDrawable>();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		canvas.save();
		canvas.rotate( 270 - rot , pivotX, pivotY);
		super.onDraw(canvas);

		map.draw(canvas);

		for (ShapeDrawable dot : points) {
			dot.draw(canvas);
		}

		canvas.restore();
		
		
		canvas.drawLine(pivotX, pivotY, pivotX
				- (float) (Math.sin(100) * (radious / 2)),
				(float) (pivotY - (Math.cos(100) * (radious / 2))), p);
		canvas.drawLine(pivotX, pivotY, pivotX
				+ (float) (Math.sin(100) * (radious / 2)),
				(float) (pivotY - (Math.cos(100) * (radious / 2))), p);
		
		canvas.drawText("▲N" + (int) rot + "°", pivotX, pivotY - (radious / 2)
				- padding, paint);
		center.draw(canvas);
		outLine.draw(canvas);
		
		invalidate();

	}

	public void setR(float r) {
		rot = r;
	}

	public void addPoints(ARSphericalView view) {

		if (view.distance <= 10000) {

			double distance = view.distance / max_distance * 100;
			int left = (int) (distance * Math.sin(view.azimuth + 110));
			int top = (int) (distance * Math.cos(view.azimuth +110));

			int x = left;
			int y = top;
			if (view.azimuth == 0 || view.azimuth == 360) {
				x = pivotX;
				y = pivotY - top;
			} else if (view.azimuth < 90 && view.azimuth > 0) {
				x = pivotX + left;
				y = pivotY - top;
			} else if (view.azimuth == 90) {
				x = pivotX + left;
				y = pivotY;
			} else if (view.azimuth > 90 && view.azimuth < 180) {
				x = pivotX + left;
				y = pivotY + top;
			} else if (view.azimuth == 180) {
				x = pivotX;
				y = pivotY + top;
			} else if (view.azimuth > 180 && view.azimuth < 270) {
				x = pivotX - left;
				y = pivotY + top;
			} else if (view.azimuth == 270) {
				x = pivotX - left;
				y = pivotY;
			} else {
				x = pivotX - left;
				y = pivotY - top;
			}

			ShapeDrawable dot = new ShapeDrawable(new OvalShape());
			dot.setBounds(x, y, x + 10, y + 10);
			dot.getPaint().setColor(Color.rgb(3, 187, 255));
			dot.setAlpha(85);
			points.add(dot);
		}
	}
}
